<?php 
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: nonant <nonant@thinksns.com>
// +----------------------------------------------------------------------
// 

//动态服务
class InviteService extends Service{
    public function __construct($data) {
		//2.服务初始化
		$this->run();
    }


	//运行服务，系统服务自动运行
	public function run(){

	}
	
	//获取邀请地址
	public function getCode($uid,$type,$mail,$data){
		$code = $this->bulidcode($uid,$type,$mail,$data);
		if($code){
			return U('home/Public/init',array('invitecode'=>$code));
		}
	}
	
	//获取详细信息
	public function getData($code){
		$code = h($code);
		$info = model('Invite')->where("passkey='{$code}'")->field('uid,touser,data,type,passkey,cTime')->find();
		if($info){
			$info['data'] = unserialize($info['data']);
			return $info;
		}else{
			return '';
		}
	}
	
	//验证代码的正确性
	public function checkCode($code,$type,$touser){
		return model('Invite')->where("passkey='{$code}' AND type='{$type}' AND touser='{$touser}'")->count();
	}
	
	//生成标识码
	public function bulidcode($uid,$type,$touser,$data){
		$pInvite = M('invite');
		$map['uid']  = $uid;
		$map['type'] = $type;
		$map['touser'] = $touser;
		$map['data'] = serialize($data);
		$info = $pInvite->where($map)->find();
		if($info['id']){
			return $info['passkey'];
		}else{
			$addmap['cTime'] = time();
			$addmap = array_merge($map,$addmap);
			$id = $pInvite->add($addmap);
			if($id){
				$code = md5($id.'hi#^!@*#%^!@#');
				$pInvite->setField('passkey',$code,'id='.$id);
				return $code;
			}
		}

 	}
 	
	//启动服务，未编码
	public function _start(){
		return true;
	}
	
	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//安装服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>